package omq.panel;

import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import omq.api.logger.OMQLogger;
import omq.screen.OmqTesterMainScreen;

import component.OmqButton;

import constant.OmqTesterClientConst;

public class ClearPreferencesPanel extends OmqPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1732788936335583062L;
	private JLabel infoLbl;
	private OmqButton clearBtn;
	private Preferences myPreferences = null;
	
	public ClearPreferencesPanel(LayoutManager arg0, boolean arg1) {
		super(arg0, arg1);
		initialize();
	}

	public ClearPreferencesPanel(LayoutManager arg0) {
		super(arg0);
		initialize();
	}

	public ClearPreferencesPanel(boolean arg0) {
		super(arg0);
		initialize();
	}

	public ClearPreferencesPanel() {
		super();
		initialize();
	}

	private void initialize(){
		myPreferences = Preferences.userNodeForPackage(OmqTesterMainScreen.class);
		
		add(getInfoLbl());
		add(getClearBtn());
	}

	public JLabel getInfoLbl() {
		if(infoLbl == null){
			infoLbl = new JLabel("<html><body>This is your last chance to walk away!<br>By pressing the 'Clear' button, the preferences will be erased and you will have to fill it all over again.<br>" +
					"<table style='color:#456750;'><tr><td>User</td><td>:</td><td>" + myPreferences.get(OmqTesterClientConst.PREF_USER_NAME,OmqTesterClientConst.PREF_USER_NAME)  + "</td></tr>" +
					"<tr><td>Password</td><td>:</td><td>" + myPreferences.get(OmqTesterClientConst.PREF_PASSWORD,OmqTesterClientConst.PREF_PASSWORD)  + "</td></tr>" +
					"<tr><td>Queue manager</td><td>:</td><td>" + myPreferences.get(OmqTesterClientConst.PREF_QUEUE_MGR,OmqTesterClientConst.PREF_QUEUE_MGR)  + "</td></tr>" +
					"<tr><td>Queue name</td><td>:</td><td>" + myPreferences.get(OmqTesterClientConst.PREF_QUEUE_NAME,OmqTesterClientConst.PREF_QUEUE_NAME)  + "</td></tr>" +
					"</table>" +
					"<br><br>Please choose wisely - Press if you sure or - Go to another page!</body></html>");
		}
		return infoLbl;
	}

	public OmqButton getClearBtn() {
		if(clearBtn == null){
			clearBtn = new OmqButton("Clear");
			clearBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					OMQLogger.getInstance().debug("Now you've done it - i am erasing all the data saved in me!");
					waitForInfo(true);
					try {
						myPreferences.clear();
						getInfoLbl().setText("<html><body>Now you have done it! <br> The preferences are now erased, as you ordered.");
						getClearBtn().setVisible(false);
					} catch (BackingStoreException e) {
						OMQLogger.getInstance().fatal(e.getMessage());
						JOptionPane.showMessageDialog(new JFrame(), e.getMessage(),"Preferences error",JOptionPane.ERROR_MESSAGE);
					}
					waitForInfo(false);
				}
			});
		}
		return clearBtn;
	}

	public void showOpenInfo(){
		getInfoLbl().setText("<html><body>This is your last chance to walk away!<br>By pressing the 'Clear' button, the preferences will be erased and you will have to fill it all over again.<br>" +
					"<table style='color:#456750;'><tr><td>User</td><td>:</td><td>" + myPreferences.get(OmqTesterClientConst.PREF_USER_NAME,OmqTesterClientConst.PREF_USER_NAME)  + "</td></tr>" +
					"<tr><td>Password</td><td>:</td><td>" + myPreferences.get(OmqTesterClientConst.PREF_PASSWORD,OmqTesterClientConst.PREF_PASSWORD)  + "</td></tr>" +
					"<tr><td>Queue manager</td><td>:</td><td>" + myPreferences.get(OmqTesterClientConst.PREF_QUEUE_MGR,OmqTesterClientConst.PREF_QUEUE_MGR)  + "</td></tr>" +
					"<tr><td>Queue name</td><td>:</td><td>" + myPreferences.get(OmqTesterClientConst.PREF_QUEUE_NAME,OmqTesterClientConst.PREF_QUEUE_NAME)  + "</td></tr>" +
					"</table>" +
					"<br><br>Please choose wisely - Press if you sure or - Go to another page!</body></html>");
		getClearBtn().setVisible(true);
	}
}
